package com.itxiaowu.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/**
 * 七牛   文件上传和删除文件
 */
public class QiniuUtils {

    private static String accessKey = "LgZsjscf1oO-2-tZ0bpQGiIhrg2qxxn097Nr5WfP";
    private static String secretKty = "Gtq26P1YHqearDe4i6u-77FGBY-l-MuJbg1jBBIO";
    private static String bucket = "first_health";

    /**
     * 文件上传  --> 文件路径上传
     * @param filePath
     * @param fileName
     */
    public static void upload2Qiniu(String filePath,String fileName){

        //构造一个带指定Zone对象的配置
        Configuration cfg = new Configuration(Zone.zone0());
        UploadManager uploadManager = new UploadManager(cfg);

        Auth auth = Auth.create(accessKey, secretKty);

        String upToken = auth.uploadToken(bucket);

        try {
            Response resp = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功的结果
            new Gson().fromJson(resp.bodyString(), DefaultPutRet.class);
        } catch (QiniuException e) {
            Response r = e.response;

            try {
                System.out.println(r.bodyString());
            } catch (QiniuException ex) {
                //ignore
            }
        }

    }

    /**
     * 上传文件  ---> 字节流上传
     */

    public static void upload2Qiniu(byte[] bytes ,String fileName){

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());

        UploadManager uploadManager = new UploadManager(cfg);

        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = fileName;

        Auth auth = Auth.create(accessKey, secretKty);

        String upToken = auth.uploadToken(bucket);

        try {
            Response put = uploadManager.put(bytes, key, upToken);

            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(put.bodyString(), DefaultPutRet.class);

            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException e) {
            Response response = e.response;
            System.out.println(response.toString());
            try {
                System.out.println(response.bodyString());
            } catch (QiniuException ex) {

            }
        }

    }

    /**
     * 删除文件
     */
    public static void deleteFileFromQiniu(String fileName){

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        String key = fileName;

        Auth auth = Auth.create(accessKey, secretKty);

        BucketManager bucketManager = new BucketManager(auth, cfg);

        try {
            bucketManager.delete(bucket,key);
        } catch (QiniuException e) {
            //如果遭遇异常，说明删除失败
            System.err.println(e.code());
            System.err.println(e.response.toString());
        }

    }

}
